/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Screen;

import ClassFrame.Button;
import ClassFrame.InputKey;
import ClassFrame.Resource;
import GamePlay.CanvasGame;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.TextBox;

/**
 *
 * @author QuyetNM1
 */
public class Menu extends IScreen {

    public static final int CHOI_GAME = 0;
    public static final int KI_LUC = 1;
    public static final int THOAT = 2;
    public static final int MAX_MENU = 3;
    private Button btnChoiGame, btnKyLuc, btnThoat, btnAbout;
    private TextBox tb;
    
    
    
    public static boolean isTB;
    public Menu(CanvasGame _canvas) {
        super(_canvas);
    }

    public void initComponents() {
        
        canvas.getResource().unLoadSound(Resource.SOUND_PLAY);
        canvas.getResource().playSound(Resource.SOUND_MENU, -1);
        
        btnChoiGame = new Button("", Resource.IMG_BTN_CHOIGAME, 55, 130);
        btnThoat = new Button("", Resource.IMG_BTN_EXIT, 85, 270);

        btnKyLuc = new Button("", Resource.IMG_BTN_HIGHSCORE, 40, 265);
        btnAbout = new Button("", Resource.IMG_BTN_ABOUT, 8, 230);

        tb = new TextBox("Vui lòng nhập tên", canvas.getaPlayer().getName(), 8, 0);
        isTB = false;
    }

    public void load_screen() {
        canvas.getResource().loadArray(Resource.menuArrayImages);
    }

    public void un_load_screen() {
        canvas.getResource().unLoadArray(Resource.menuArrayImages);
    }

    public void keyEvent(int keycode, int event) {
        if (event == InputKey.KEY_EVENT_DOWN) {

            switch (keycode) {
                case InputKey.KEY_UP:
                    break;
                case InputKey.KEY_DOWN:
                    break;

                case InputKey.KEY_LEFT_SOFTKEY:
                    break;
                case InputKey.KEY_RIGHT_SOFTKEY:
                    CanvasGame.running = false;
                    break;
                case InputKey.KEY_OK:
                    break;
            }
        }
    }

    public void pointerEvent(int x, int y, int event) {

        switch (event) {
            case InputKey.POINTER_EVENT_DOWN:
                
                if (canvas.checkButton(x, y, btnChoiGame)) {
                    btnChoiGame.setState(Button.HOLD_CLICK);
                    canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                }
                if (canvas.checkButton(x, y, btnThoat)) {
                    btnThoat.setState(Button.HOLD_CLICK);
                }

                if (canvas.checkButton(x, y, btnAbout)) {
                    btnAbout.setState(Button.HOLD_CLICK);
                    canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                } else if (canvas.checkButton(x, y, btnKyLuc)) {
                    btnKyLuc.setState(Button.HOLD_CLICK);
                    canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                }
                
                if(canvas.checkRegion(x, y, xSound, ySound, 45, 45)){
                    CanvasGame.isSound = !CanvasGame.isSound;
                    if(CanvasGame.isSound){
                        CanvasGame.musicLevel = 80;
                        canvas.getResource().sounds[Resource.SOUND_MENU].setVolumeLevel(CanvasGame.musicLevel);
                    } else {
                        CanvasGame.musicLevel = 0;
                        canvas.getResource().sounds[Resource.SOUND_MENU].setVolumeLevel(CanvasGame.musicLevel);
                    }
                }
                
                break;
            case InputKey.POINTER_EVENT_UP:
                //Button choi game
                if (canvas.checkButton(x, y, btnChoiGame) && btnChoiGame.getState() == Button.HOLD_CLICK) {
                    canvas.getMIDletGame().getDisplay().setCurrent(tb);
                    isTB = true;
                    tb.addCommand(new Command("OK", Command.OK, 1));
                    tb.setCommandListener(new CommandListener() {

                        public void commandAction(Command c, Displayable d) {
                            if (c.getCommandType() == Command.OK) {
                                canvas.getMIDletGame().getDisplay().setCurrent(canvas);
                                canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                                canvas.getaPlayer().setName(tb.getString());
                                canvas.setScreen(new Welcome(canvas));
                                isTB = false;
                            }
                        }
                    });
                }

                if (canvas.checkButton(x, y, btnThoat) && btnThoat.getState() == Button.HOLD_CLICK) {
                    canvas.running = false;
                }

                if (canvas.checkButton(x, y, btnKyLuc) && btnKyLuc.getState() == Button.HOLD_CLICK) {
                    canvas.setScreen(new Score(canvas));
                } else if (canvas.checkButton(x, y, btnAbout) && btnAbout.getState() == Button.HOLD_CLICK) {
                  canvas.setScreen(new About(canvas));
                }



                btnChoiGame.setState(Button.ENABLE);
                btnKyLuc.setState(Button.ENABLE);
                btnThoat.setState(Button.ENABLE);
                btnAbout.setState(Button.ENABLE);
                break;
        }
    }

    public void update() {
    }
    private int count = 0;
    public static final int xSound = 18;
    public static final int ySound = 277-100;

    public void paint(Graphics g) {
        canvas.getImage().drawImage(g, canvas, Resource.IMG_BG_MENU + count%16/8, 0, 0);
        
        if(CanvasGame.isSound){
            canvas.getImage().drawImagePart(g, canvas, Resource.IMG_BTN_SOUND, xSound, ySound, 0, 0, 45, 45);
        } else {
            canvas.getImage().drawImagePart(g, canvas, Resource.IMG_BTN_SOUND, xSound, ySound, 0, 45, 45, 45);
        }

        btnChoiGame.drawButton(g, canvas);
        btnThoat.drawButton(g, canvas);
        btnKyLuc.drawButton(g, canvas);
        btnAbout.drawButton(g, canvas);
        count++;
    }
}
